package com.logbug.concurrence;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程操作资源类
 */
class SharedResource {
    /**
     * 1代表A线程
     * 2代表B线程
     * 3代表C线程
     */
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition c1 = lock.newCondition();
    private final Condition c2 = lock.newCondition();
    private final Condition c3 = lock.newCondition();
    volatile int num=0;

    public void start() {
        lock.lock();
        System.out.println("start t1");
        c1.signal();
        lock.unlock();
    }

    public void print5() {
        lock.lock();
        if(num==5){
            Thread.currentThread().interrupt();
            return;
        }
        num++;
        try {
            System.out.println("t1 is waiting.");
            c1.await();
            // 干活
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 通知
            c2.signal();// 通知B
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void print10() {
        lock.lock();
        try {
            // 判断
            // 等待
            c2.await();
            // 干活
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 通知
            c3.signal();// 通知C
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void print15() {
        lock.lock();
        try {
            // 判断
            // 等待
            c3.await();
            // 干活
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 通知
            c1.signal();// 通知A
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

/**
 * @Description 题目：多线程之前按顺序调用，实现A->B->C三个线程启动，要求如下：
 * A打印5次，B打印10次，C打印15次
 * 紧接着
 * A打印5次，B打印10次，C打印15次
 * ......
 * 来5轮
 */
public class ReentrantLockCondition {
    public static void main(String[] args) {
        SharedResource sharedResource = new SharedResource();

        Thread t1 = new Thread(() -> {
            while (true) {
                sharedResource.print5();
            }
        }, "A");
        t1.start();

        new Thread(() -> {
            while (true) {
                sharedResource.print10();
            }
        }, "B").start();
        new Thread(() -> {
            while (true) {
                sharedResource.print15();
            }
        }, "C").start();
//        new Thread(() -> sharedResource.print10(), "B").start();
//        new Thread(() -> sharedResource.print15(), "C").start();
//        int num = 5;
//        for (int i = 1; i <= num; i++) {
//
//        }

        while (true) {
            if (t1.getState() == Thread.State.WAITING) {
                sharedResource.start();
                break;
            }
        }

//        sharedResource.start();

    }
}

